package com.e.ticket;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.e.Adapter.OrderAdapter;
import com.e.Adapter.TickeAdapter;
import com.e.Dao.UserDao;
import com.e.Pojo.Order;
import com.e.SQL.UserDBHepler;

import java.util.List;

public class Ticket_Order extends Fragment {
    private ListView list_order;
    private UserDao userDao;
    private List<Order> orderList;
    final String FILE_NAME="user";
    private OrderAdapter adapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = LayoutInflater.from(this.getActivity()).inflate(R.layout.activity_ticket_order, null);
        return v;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        list_order = (ListView) getActivity().findViewById(R.id.list_order);
        userDao  = new UserDao(new UserDBHepler(getActivity()));
        SharedPreferences sp =getActivity().getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        String sno = sp.getString("sno","");
        orderList = userDao.findTicketBySno(sno);
        System.out.println(orderList.toString());
        adapter = new OrderAdapter(getActivity(),orderList);
        list_order.setAdapter(adapter);

        list_order.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                 Order order = orderList.get(i);
                AlertDialog dialog = new AlertDialog.Builder(getActivity())
                           .setTitle("取消预约")
                           .setMessage("是否取消预约时间为:"+order.getDate1()+"  "+order.getStart()+"----->"+order.getLast()+"的车次")
                           .setNegativeButton("返回", new DialogInterface.OnClickListener() {
                               @Override
                               public void onClick(DialogInterface dialogInterface, int i) {
                                   dialogInterface.dismiss();
                               }
                           })
                           .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                               @Override
                               public void onClick(DialogInterface dialogInterface, int i) {
                                   dialogInterface.dismiss();
                                   if (userDao.deleteFindById(order.getId())) {
                                       Toast.makeText(view.getContext(), "取消成功", Toast.LENGTH_SHORT).show();
                                       adapter.notifyDataSetChanged();
                                       getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.fl_content,new Ticket_Order()).commit();
                                   }
                                   else
                                       Toast.makeText(view.getContext(), "取消预约失败", Toast.LENGTH_SHORT).show();
                               }
                           })
                           .create();
                   dialog.show();
                   return true;
            }
        });


    }



//    @Override
//    public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
//        inflater.inflate(R.menu.back, menu);
//        super.onCreateOptionsMenu(menu,inflater);
//    }
//    //onOptionsItemSelected
//
//    @Override
//    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
//        switch (item.getItemId()) {
//            case R.id.action_cart://监听菜单按钮
//                System.out.println("==============>>>>>>>>>>>>");
//                //返回back监听事件
//                Toast.makeText(this.getActivity(),"99999999999",Toast.LENGTH_SHORT).show();
//
////                Intent intent =new Intent(Update_password.this,Own.class);
////                startActivity(intent);
//                break;
//        }
//        return super.onOptionsItemSelected(item);
//    }

}